import pygame.mouse as pm
import pygame.key as pk
import pygame.event as pe
import pygame.sprite as ps
from pygame.locals import QUIT, K_ESCAPE, MOUSEBUTTONDOWN
from sys import exit
from lib.ZScene import ZScene
from res.fun import _create_rect, _out_screen_bottom, _get_right_bottom , _get_center
from res.glo import color
from res.conf import gametick, items
from assets.script.plane import Plane
from res.conf import window_size, enemys
from assets.script.enemy import Enemy
from lib.ZLabel import ZLabel
from res.lang import ch
from assets.script.panel.bottombar import BottomBar
from res.conf import score
from assets.script.item import Item


class Game(ZScene):
    def __init__(self):
        super(Game, self).__init__()
        self.game_over = False
        self.tick = gametick
        self.score = score
        self.background = _create_rect(color['background'], window_size)
        self.plane = Plane()
        self.enemys = []
        self.enemy_num = enemys
        self.labelscore = ZLabel(self.screen)
        self.labelscore.text = ch['scorse']
        self.labelpause = ZLabel(self.screen)
        self.labelpause.text = ch['button']['pause']
        self.labelpause._pos = _get_center(self.labelpause.rect)
        self.labelscore._pos = 10, 20
        self.labelhealth = ZLabel(self.screen)
        self.labelhealth.text = ch['health']
        self.labelhealth._pos = window_size[0] / 2 - self.labelhealth.rect.w / 2 , window_size[1] - self.labelhealth._size[1] - 20
        self.panel = BottomBar(self.screen)
        self.panel.stop()
        self.items = ps.Group()
        self.items_num = items

    def draw(self):
        self.iswin()
        self.ispause()
        self.stopsprites()
        self.plane.draw()
        self.labelscore.draw()
        self.labelhealth.draw()
        self.panel.draw()
        self.labelpause.draw()
        self.items.draw(self.screen)
        for enemy in self.enemys:
            enemy.draw()

    def update(self):
        mouse_pos = pm.get_pos()
        mouse_pressed = pm.get_pressed()
        keys = pk.get_pressed()
        for event in pe.get():
            if keys[K_ESCAPE]:
                self.pause() if self.isRunning else self.play()
            if event.type == QUIT:
                exit()
            elif event.type == MOUSEBUTTONDOWN:
                if self.panel.click(mouse_pos, mouse_pressed):
                    self.restart()
            else:
                self.panel.update(mouse_pos)
        if self.isRunning :
            self.collidetest()
            self.plane.update(keys)
            self.items.update()
            for enemy in self.enemys:
                enemy.update()

    def collidetest(self):
        for enemy in self.enemys:
            if not enemy.isRunning:
                self.enemys.remove(enemy)
            if self.plane.rect.colliderect(enemy):
                self.plane.hit(enemy)
                if enemy in self.enemys:
                    self.enemys.remove(enemy)
            if _out_screen_bottom(enemy.rect):
                if enemy in self.enemys :
                    self.enemys.remove(enemy)
            for bullet in self.plane.bullets:
                if bullet.rect.colliderect(enemy):
                    self.plane.bullets.remove(bullet)
                    if enemy in self.enemys: enemy.hit(bullet)
                    if enemy.isRunning == False:
                        self.score += enemy.score
        for item in self.items:
            if _out_screen_bottom(item.rect):
                self.items.remove(item)
            if self.plane.rect.colliderect(item):
                self.plane.health += item.health
                self.items.remove(item)

    def enemysdraw(self):
        if len(self.enemys) < self.enemy_num:
            self.enemys.append(Enemy())

    def itemsdraw(self):
        if len(self.items) < self.items_num:
            self.items.add(Item())

    def iswin(self):
        if self.plane.health <= 0:
            self.game_over = False
        else:
            self.game_over = True

    def stopsprites(self):
        if self.game_over:
            self.screen.blit(self.background, [0, 0])
            self.plane.start()
            self.labelscore.start()
            self.labelhealth.start()
            self.items.draw(self.screen)
            self.labelscore.text = ch['scorse'] + str(self.score)
            self.labelhealth.text = ch['health'] + str(self.plane.health)
            self.enemysdraw()
            self.itemsdraw()
        else:
            self.labelhealth.text = ch['health'] + str(0)
            self.plane.stop()
            self.labelscore.stop()
            for enemy in self.enemys:
                enemy.stop()
            for item in self.items:
                item.stop()
            self.panel.start()

    def restart(self):
        self.plane.reset()
        self.enemys = []
        self.items.empty()
        self.score = score
        self.plane.start()
        self.panel.stop()

    def ispause(self):
        if not self.isRunning:
            self.labelpause.start()
        else : self.labelpause.stop()